package com.example.hms.awareness;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.widget.Toast;
import androidx.appcompat.widget.SwitchCompat;
import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.OnFailureListener;
import com.huawei.hmf.tasks.OnSuccessListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.kit.awareness.Awareness;
import com.huawei.hms.kit.awareness.barrier.TimeBarrier;
import com.huawei.hms.kit.awareness.barrier.AwarenessBarrier;
import com.huawei.hms.kit.awareness.barrier.BarrierUpdateRequest;

import java.util.TimeZone;

public class BarrierUtil {
    private static final String TAG = BarrierUtil.class.getSimpleName();
    private static String timeBarrierLabel0 = "study Time barrier";
    private static String timeBarrierLabel1 = "3D Time barrier";
    private static String timeBarrierLabel2 = "we chat";
    public static void createBarrier2(Activity activity,
                                      SwitchCompat openOrClose) {
        TimeZone timeZone=TimeZone.getDefault();
        final  long startTimeOfDay2 = 60*60*1000*6;
        final  long stopTimeOfDay2=60*60*1000*7;
        AwarenessBarrier TimesBarrier2;
        TimesBarrier2 = TimeBarrier.duringPeriodOfDay(timeZone,startTimeOfDay2,stopTimeOfDay2);
        final String BARRIER_RECEIVER_ACTION = activity.getPackageName() + ".Time_BARRIER_RECEIVER_ACTION";
        Log.d(TAG, BARRIER_RECEIVER_ACTION);
        Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
        PendingIntent pendingIntent=
                PendingIntent.getBroadcast(activity, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        TimeBarrierReceiver barrierReceiver2 = new TimeBarrierReceiver();

        activity.registerReceiver(barrierReceiver2, new IntentFilter(BARRIER_RECEIVER_ACTION));

        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request2= builder.addBarrier(timeBarrierLabel2, TimesBarrier2, pendingIntent).build();
        Awareness.getBarrierClient(activity)
                .updateBarriers(request2)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "add we chat barrier success");

                        Toast.makeText(activity, "微信定时开启成功，时间6-7点", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        Log.e(TAG, "add barrier failed", e);
                        Toast.makeText(activity, "微信定时开启失败", Toast.LENGTH_SHORT).show();
                        openOrClose.setChecked(false);
                    }
                });

    }
    public static void createBarrier0(Activity activity,
                                     SwitchCompat openOrClose) {
        final  long startTimeOfDay0 = 60*60*1000*7;
        final  long stopTimeOfDay0=60*60*1000*8;
        TimeZone timeZone=TimeZone.getDefault();

        AwarenessBarrier TimesBarrier0;
        TimesBarrier0 = TimeBarrier.duringPeriodOfDay(timeZone,startTimeOfDay0,stopTimeOfDay0);

        final String BARRIER_RECEIVER_ACTION = activity.getPackageName() + ".Time_BARRIER_RECEIVER_ACTION";
        Log.d(TAG, BARRIER_RECEIVER_ACTION);
        Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
        PendingIntent pendingIntent=
            PendingIntent.getBroadcast(activity, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        TimeBarrierReceiver barrierReceiver0 = new TimeBarrierReceiver();

        activity.registerReceiver(barrierReceiver0, new IntentFilter(BARRIER_RECEIVER_ACTION));

        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request0= builder.addBarrier(timeBarrierLabel0, TimesBarrier0, pendingIntent).build();
        Awareness.getBarrierClient(activity)
            .updateBarriers(request0)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "add study barrier success");

                    Toast.makeText(activity, "学习强国定时开启成功，时间为7-8点", Toast.LENGTH_SHORT).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    Log.e(TAG, "add barrier failed", e);
                    Toast.makeText(activity, "学习强国定时开启失败", Toast.LENGTH_SHORT).show();
                    openOrClose.setChecked(false);
                }
            });


    }
    public static void createBarrier1(Activity activity,
                                      SwitchCompat openOrClose) {
            TimeZone timeZone=TimeZone.getDefault();
        final  long startTimeOfDay1 = 60*60*1000*10;
        final  long stopTimeOfDay1=60*60*1000*11;
        AwarenessBarrier TimesBarrier1;
        TimesBarrier1 = TimeBarrier.duringPeriodOfDay(timeZone,startTimeOfDay1,stopTimeOfDay1);
        final String BARRIER_RECEIVER_ACTION = activity.getPackageName() + ".Time_BARRIER_RECEIVER_ACTION";
        Log.d(TAG, BARRIER_RECEIVER_ACTION);
        Intent intent = new Intent(BARRIER_RECEIVER_ACTION);
        PendingIntent pendingIntent=
                PendingIntent.getBroadcast(activity, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        TimeBarrierReceiver barrierReceiver1 = new TimeBarrierReceiver();

        activity.registerReceiver(barrierReceiver1, new IntentFilter(BARRIER_RECEIVER_ACTION));

        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request1= builder.addBarrier(timeBarrierLabel1, TimesBarrier1, pendingIntent).build();
        Awareness.getBarrierClient(activity)
                .updateBarriers(request1)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "add 3D barrier success");

                        Toast.makeText(activity, "3D建模定时开启成功，时间10-11点", Toast.LENGTH_SHORT).show();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        Log.e(TAG, "add barrier failed", e);
                        Toast.makeText(activity, "3D建模定时开启失败", Toast.LENGTH_SHORT).show();
                        openOrClose.setChecked(false);
                    }
                });

    }

    public static void deleteBarrier(Activity activity) {
        BarrierUpdateRequest.Builder builder = new BarrierUpdateRequest.Builder();
        BarrierUpdateRequest request = builder.deleteBarrier(timeBarrierLabel0).build();
        Awareness.getBarrierClient(activity)
            .updateBarriers(request)
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.d(TAG, "delete barrier success");
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    Log.e(TAG, "delete barrier failed", e);
                }
            })
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(Task<Void> task) {
                    Log.d(TAG, "barrier stop");
                }
            });
    }
}
